package com.onlineClass.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.onlineClass.edu.cache.LocalCache;
import com.onlineClass.edu.constant.LocalCacheKeyConstants;
import com.onlineClass.edu.entity.EduCourse;
import com.onlineClass.edu.entity.EduCourseClass;
import com.onlineClass.edu.entity.dto.response.EduCourseClassRespDto;
import com.onlineClass.edu.entity.dto.response.EduCourseRespDto;
import com.onlineClass.edu.service.IEduCourseClassService;
import com.onlineClass.edu.service.IEduCourseService;
import com.onlineClass.edu.service.IStartPreheatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 邓辉
 * @ClassName StartPreheatServiceImpl
 * @date 2024-04-30 21:59
 */
@Service
@Slf4j
public class StartPreheatServiceImpl implements IStartPreheatService {
    @Autowired
    private IEduCourseClassService iEduCourseClassService;
    @Autowired
    private IEduCourseService iEduCourseService;

    /**
     * 后面还有大量的缓存预热，所以写成方法调用更便于维护
     */
    @Override
    public void initData() {
        //1.初始化课程分类加入缓存中
        long startTime = System.currentTimeMillis();
        initAllEducation();
        initAllCourses();
        long endTime = System.currentTimeMillis();
        log.info("【缓存预热时间:{}】",(endTime-startTime));

    }

    /**
     * 初始化课程分类表
     */
    private void initAllEducation() {
        List<EduCourseClass> allClasses = iEduCourseClassService.getAll();
        //do对象去敏感属性转为dto对象
        List<EduCourseClassRespDto> eduCourseClassRespDtos = BeanUtil.copyToList(allClasses, EduCourseClassRespDto.class);
        //缓存里存的是脱敏的dto
        LocalCache.put(LocalCacheKeyConstants.COURSE_ALLCOURSECLASSLIST, eduCourseClassRespDtos);
    }

    /**
     * 初始化课程
     */
    private void initAllCourses(){
        List<EduCourse> allClasses = iEduCourseService.getAll();
        //do对象去敏感属性转为dto对象
        List<EduCourseRespDto> eduCourseRespDtos = BeanUtil.copyToList(allClasses, EduCourseRespDto.class);
        //缓存里存的是脱敏的dto
        LocalCache.put(LocalCacheKeyConstants.COURSE_ALLCOURSELIST, eduCourseRespDtos);
    }

}
